package com.wx.mall.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.wx.mall.common.api.CommonPage;
import com.wx.mall.common.api.PageVo;
import com.wx.mall.sys.entity.LoginLog;
import com.wx.mall.sys.mapper.LoginLogMapper;
import com.wx.mall.sys.service.ILoginLogService;
import com.wx.mall.common.vo.sys.LoginLogVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 登录日志 服务实现类
 * </p>
 *
 * @author ljl
 * @since 2020-03-30
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {
//    @Autowired
//    private RestTemplateBuilder builder;

    @Override
    public CommonPage<LoginLog> pageList(PageVo vo) {
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<>();
        // 按登录日期倒序查询
        wrapper.orderByDesc("login_time");

        List<LoginLog> loginLogs = baseMapper.selectList(wrapper);
        return CommonPage.restPage(loginLogs);
    }

    @Override
    public Integer save(LoginLogVO vo) {
//        String ip = getIPAddress(request);
//        System.out.println("调用了登录日志保存服务");
//        LoginLog log = new LoginLog();
//        log.setLoginName("测试：默认用户");
//        log.setIp(ip);
//        log.setBrowserName(getBrowser(request));
//        log.setLocation(getRegionByIp(ip));
//        // 获取当前时间
//        // 方式一：Date对象
//        Date date = new Date();
        // 方式二：Calendar对象
//        Calendar calendar = Calendar.getInstance();
//        Date date = calendar.getTime();
//        log.setLoginTime(date);
        LoginLog loginLog = new LoginLog();
        //将vo中的数据复制到实体类中
        BeanUtils.copyProperties(vo, loginLog);

        return baseMapper.insert(loginLog);
    }
}
